package net.xxcxcxcx.xc_enhancedmob.render.boss_bar.complete.client;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.column.CustomBossBarColumn;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.column.client.ClientCustomBossBarColumn;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.complete.BaseCustomBossBarComplete;

public class ClientCustomBossBarComplete extends BaseCustomBossBarComplete implements ClientBaseCustomBossBarComplete {
    private int entity;

    public ClientCustomBossBarComplete(int entity, NbtCompound nbtCompound) {
        readCustomDataFromNbt(nbtCompound);
        this.entity = entity;
    }

    @Override
    public void readCustomDataFromNbt(NbtCompound nbt) {
        for (String s : nbt.getKeys()) {
            if (s.equals("name")) {
                setName(Text.Serializer.fromJson(nbt.getString("name")));
                continue;
            }
            if (s.equals("visible")) {
                continue;
            }
            if (s.equals("showname")) {
                setShowname(nbt.getBoolean("showname"));
                continue;
            }
            if (s.equals("SortCount")) {
                setSortCount(nbt.getInt("SortCount"));
                continue;
            }
            getBossColumn().add(new ClientCustomBossBarColumn(nbt.getCompound(s)));
        }
    }

    @Override
    public int getEntity() {
        return entity;
    }

    @Override
    public void setEntity(int entity) {
        this.entity = entity;
    }

    //获取当前完整血条的像素宽度
    @Override
    public int getWidth() {
        int maxcount = -1;
        for (CustomBossBarColumn customBossBarColumn : getBossColumn()) {
            if (customBossBarColumn instanceof ClientCustomBossBarColumn customBossBarColumn1) {
                maxcount = Math.max(maxcount, customBossBarColumn1.getWidth());
            } else break;
        }
        return maxcount * getBossColumn().size();
    }

    //获取当前完整血条的像素高度
    @Override
    public int getHeight() {
        int maxcount = -1;
        for (CustomBossBarColumn customBossBarColumn : getBossColumn()) {
            if (customBossBarColumn instanceof ClientCustomBossBarColumn customBossBarColumn1) {
                maxcount = Math.max(maxcount, customBossBarColumn1.getHeight());
            } else break;
        }
        return maxcount;
    }
}
